package cn.ycc1.boot3mvc.controller;

import cn.ycc1.boot3mvc.common.Result;
import cn.ycc1.boot3mvc.user.vo.UserExportVo;
import cn.ycc1.boot3mvc.user.vo.UserQueryVo;
import cn.ycc1.boot3mvc.user.domain.User;
import cn.ycc1.boot3mvc.user.service.UserService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author ycc
 * @date 2023/11/18
 */
@Tag(name = "用户管理")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    // http://localhost:8081/swagger-ui/index.html
    @Autowired
    UserService userService;

    @Operation(summary = "获取用户详情")
    @GetMapping("/detail/{id}")
    public Result<User> detail(@PathVariable("id") Long id) {
        User user = userService.getDetail(id);
        return new Result<>(200, "成功", user);
    }

    @PostMapping("/page")
    @Operation(summary = "获取用户的分页列表")
    public Result<IPage<User>> page(@RequestBody UserQueryVo queryVo) {
        IPage<User> list = userService.getPage(queryVo);
        return new Result<>(200, "成功", list);
    }

    @PostMapping ("/export")
    @Operation(summary = "用户信息导出")
    public void export() {
        List<UserExportVo> dataList = userService.getList(); // 从数据库中获取所有数据
        EasyExcel.write("用户信息.xlsx", UserExportVo.class).sheet("模板").doWrite(dataList); // 将数据写入到Excel文件中
    }

    @GetMapping("/export2")
    public ResponseEntity<byte[]> export2(HttpServletRequest request) throws IOException {
//    public ResponseEntity<ExcelWriter> export2(HttpServletRequest request) throws IOException {
        // 准备数据
        // List<User> userList = List.of(new User(1L, "张三", 20), new User(2L, "李四", 25));
        List<UserExportVo> dataList = userService.getList();

        // 设置导出文件名
        String fileName = "用户信息.xlsx";
        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=" + fileName);
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        // 将数据写入到Excel文件中
        ExcelWriter excelWriter = EasyExcel.write().build();
        byte[] bytes = null; // 暂时注释报错
//         byte[] bytes = EasyExcel.write().build();

        // 假设你的ExcelWriter对象名为excelWriter
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        try (SXSSFWorkbook workbook = new SXSSFWorkbook()) { // SXSSFWorkbook会将数据写入到临时文件中，不会占用过多内存
//            excelWriter.write(workbook); // 将数据写入到workbook中
//            workbook.write(outputStream); // 将workbook写入到outputStream中
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        byte[] bytes = outputStream.toByteArray(); // 将outputStream转换为字节数组

        return ResponseEntity.ok()
                .headers(headers)
                .body(bytes);
    }
}
